//
//  promotionTVCell3.m
//  UIScrollView的快速使用
//
//  Created by hongfei tam on 2019/3/8.
//  Copyright © 2019 YQ05. All rights reserved.
//

#import "promotionTVCell3.h"
#import "../MKConstant.h"

@interface promotionTVCell3()<UITextFieldDelegate>{
    UIView *BGview;
    UITextField *nameTF;
    UITextField *phoneTF;
    UITextField *emailTF;
    UILabel *sexLabel;
    UISwitch *mswitch;
    BOOL issexSW1On;
    
    NSArray *serverarray;
    UIPickerView *serverPickerView;
    NSString *serverStr;
    //UIButton *submit;
    
    NSString *titleimg;
    NSString *title;
    
    NSMutableDictionary *titledic;
}

@end

@implementation promotionTVCell3

- (void)awakeFromNib {
    [super awakeFromNib];
    
    BGview = [[UIView alloc]initWithFrame:CGRectMake(20 , 20, UISCREEN_WIDTH - 40 , 350)];
    BGview.backgroundColor = [UIColor whiteColor];
    BGview.layer.shadowColor = [UIColor grayColor].CGColor;
    BGview.layer.shadowOffset = CGSizeMake(3, 3);
    BGview.layer.shadowOpacity = 0.8;
    
    [self addSubview:BGview];
    
    sexLabel.text = @"女";
    issexSW1On = true;  //设置默认状态

    // Initialization code
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    // [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}
+(instancetype)createCellWithTableView:(UITableView*)tableView{
    static NSString *ID = @"promotionTVCell3";
    promotionTVCell3 *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        // xib加载cell
        cell = [[[NSBundle mainBundle]loadNibNamed:@"promotionTVCell3" owner:nil options:nil]lastObject];
    }
    return cell;
}
-(void)loadData:(NSString*)image label:(NSString*)label andprotitledic:(NSMutableDictionary*)protitledic{
    
    titleimg = image;
    title = label;
    
    titledic = protitledic;

//    self.proLabel1.text = [protitledic valueForKey:@"Prolabel1"];
//    self.proLabel2.text = [protitledic valueForKey:@"Prolabel2"];
//    self.proLabel3.text = [protitledic valueForKey:@"Prolabel3"];
//    self.proLabel4.text = [protitledic valueForKey:@"Prolabel4"];
    
    
    // self.titleimg.image = [UIImage imageNamed:image];
    //self.protitle.text = label;
    
    //    self.phoneLabel.text = [protitledic valueForKey:@"phoneLabel"];
    //    self.emailLabel.text = [protitledic valueForKey:@"emailLabel"];
    ///////////////////////////serverarray = [protitledic valueForKey:@"serverarray"];
    //serverarray = @[@"- 請選擇 -",@"抗衰老療程",@"營養管理計劃",@"DNA檢測",@"醫療檢測",@"進階性美容療程",@"度身訂造修身/體重管理"];
    
    [self titleLabel];
    [self titleimg];
    [self nameLabel];
    [self setnameTF];
    [self phoneLabel];
    [self setphoneTF];

    [self titlesexLabel];
    [self setsexLabel];
    [self sexswitch1];
//
    [self setemailLabel];
    [self setemailTF];

    [self submitBtn];
    
}
// 输入完成之后被调用
-(void)textFieldDidEndEditing:(UITextField *)textField{
    NSLog(@"text=%@",textField.text);
}
// 点击done之后被调用
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    NSLog(@"tag=%ld",(long)textField.tag);
    [textField resignFirstResponder];//取消当前键盘输入响应
    return true;
}
-(void)titleLabel{
    //  3 字体
    UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(60, 10, UISCREEN_WIDTH - 120, 60)]; //BGview.bounds.size.width
    //label3.backgroundColor = MENUBROWN;// 背景色
    label3.text = title;//@"服務預約";// 内容
    label3.textColor = MENUBROWN; //内容颜色
    //  使用字体名称
    //label3.font = [UIFont fontWithName:@"Aril-BoldMT" size:15.0];
    label3.font = [UIFont systemFontOfSize:22.0];
    label3.numberOfLines = 2;
    //  对其方式: text内容 相对 uilabel command＋鼠标左键
    label3.textAlignment = NSTextAlignmentLeft;//NSTextAlignmentCenter;
    
    [BGview addSubview:label3];
}

-(void)titleimg{
    //  imageNamed:把一张图片读取进来
    UIImage *image1 = [UIImage imageNamed:titleimg]; //titleimg 191550736323_.pic_hd.png
    UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(20, 20, 30, 30)];
    imageview.image = image1;
    //UIImageView *imageview = [[UIImageView alloc]initWithImage:image1];
    [BGview addSubview:imageview];
}

-(void)nameLabel{
    //  3 字体
    UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(20, 80, 70, 50)];
    //label3.backgroundColor = MENUBROWN;// 背景色
    label3.text = [titledic valueForKey:@"Prolabel1"];// 内容
    label3.textColor = [UIColor grayColor]; //内容颜色
    //  使用字体名称
    //label3.font = [UIFont fontWithName:@"Aril-BoldMT" size:15.0];
    label3.font = [UIFont systemFontOfSize:14.0];
    //  对其方式: text内容 相对 uilabel command＋鼠标左键
    label3.textAlignment = NSTextAlignmentLeft;//NSTextAlignmentCenter;
    
    [BGview addSubview:label3];
}

// textfiled
-(void)setnameTF{
    nameTF = [[UITextField alloc]initWithFrame:CGRectMake(90, 80, (UISCREEN_WIDTH  - 60 - 90), 40)];
    nameTF.borderStyle = UITextBorderStyleLine;// 边框
    //text2.textColor = [UIColor redColor];// text 字体颜色 内容字体大小
    //nameTF.secureTextEntry = YES; //密码
    nameTF.font = [UIFont systemFontOfSize:17.0];
    //nameTF.placeholder = @"請輸入密碼";// 提示
    
    //    UITextField *textfield = [[UITextField alloc]initWithFrame:CGRectMake(10, 50, 300, 30)];
    //    textfield.borderStyle = UITextBorderStyleLine;// 边框
    //    textfield.textColor = [UIColor redColor];// text 字体颜色 内容字体大小
    //    textfield.font = [UIFont systemFontOfSize:17.0];
    //    textfield.placeholder = @"please enter text";// 提示
    
    nameTF.keyboardType = UIKeyboardTypeDefault;
    //    UIKeyboardTypeDefault,                // Default type for the current input method.
    //    UIKeyboardTypeASCIICapable,           // Displays a keyboard which can enter ASCII characters
    //    UIKeyboardTypeNumbersAndPunctuation,  // Numbers and assorted punctuation.
    //    UIKeyboardTypeURL,                    // A type optimized for URL entry (shows . / .com prominently).
    //    UIKeyboardTypeNumberPad,              // A number pad with locale-appropriate digits (0-9, ۰-۹, ०-९, etc.). Suitable for PIN entry.
    //    UIKeyboardTypePhonePad,               // A phone pad (1-9, *, 0, #, with letters under the numbers).
    //    UIKeyboardTypeNamePhonePad,           // A type optimized for entering a person's name or phone number.
    //    UIKeyboardTypeEmailAddress,           // A type optimized for multiple email address entry (shows space @ . prominently).
    //    UIKeyboardTypeDecimalPad NS_ENUM_AVAILABLE_IOS(4_1),   // A number pad with a decimal point.
    //    UIKeyboardTypeTwitter NS_ENUM_AVAILABLE_IOS(5_0),      // A type optimized for twitter text entry (easy access to @ #)
    //    UIKeyboardTypeWebSearch NS_ENUM_AVAILABLE_IOS(7_0),    // A default keyboard type with URL-oriented addition (shows space . prominently).
    //    UIKeyboardTypeASCIICapableNumberPad NS_ENUM_AVAILABLE_IOS(10_0),
    nameTF.returnKeyType = UIReturnKeyDone;
    //设置UITextField的边框样式
    nameTF.borderStyle = UITextBorderStyleRoundedRect;//圆角
    //    UITextBorderStyleNone, // 无边框
    //    UITextBorderStyleLine, // 有黑色边框
    //    UITextBorderStyleBezel, // 有灰色边框和阴影
    //    UITextBorderStyleRoundedRect // 圆角
    
    nameTF.clearButtonMode = UITextFieldViewModeWhileEditing; //输入框右侧删除按钮  在UITextField文本框中开始编辑的时候显示清除设置，点击清除按钮删除内容
    nameTF.delegate = self;// 谁来响应
    [BGview addSubview:nameTF];
}

-(void)phoneLabel{
    //  3 字体
    UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(20, 130, 70, 50)];
    //label3.backgroundColor = MENUBROWN;// 背景色
    label3.text = [titledic valueForKey:@"Prolabel2"];// 内容
    label3.textColor = [UIColor grayColor]; //内容颜色
    //  使用字体名称
    //label3.font = [UIFont fontWithName:@"Aril-BoldMT" size:15.0];
    label3.font = [UIFont systemFontOfSize:14.0];
    //  对其方式: text内容 相对 uilabel command＋鼠标左键
    label3.textAlignment = NSTextAlignmentLeft;//NSTextAlignmentCenter;
    
    [BGview addSubview:label3];
}

// textfiled
-(void)setphoneTF{
    phoneTF = [[UITextField alloc]initWithFrame:CGRectMake(90, 130,  (UISCREEN_WIDTH  - 60 - 90), 40)]; //_BGview.bounds.size.width -
    phoneTF.borderStyle = UITextBorderStyleLine;// 边框
    //text2.textColor = [UIColor redColor];// text 字体颜色 内容字体大小
    //nameTF.secureTextEntry = YES; //密码
    phoneTF.font = [UIFont systemFontOfSize:17.0];
    //nameTF.placeholder = @"請輸入密碼";// 提示
    
    //    UITextField *textfield = [[UITextField alloc]initWithFrame:CGRectMake(10, 50, 300, 30)];
    //    textfield.borderStyle = UITextBorderStyleLine;// 边框
    //    textfield.textColor = [UIColor redColor];// text 字体颜色 内容字体大小
    //    textfield.font = [UIFont systemFontOfSize:17.0];
    //    textfield.placeholder = @"please enter text";// 提示
    
    phoneTF.keyboardType = UIKeyboardTypePhonePad;
    
    phoneTF.returnKeyType = UIReturnKeyDone;
    //设置UITextField的边框样式
    phoneTF.borderStyle = UITextBorderStyleRoundedRect;//圆角
    
    phoneTF.clearButtonMode = UITextFieldViewModeWhileEditing; //输入框右侧删除按钮  在UITextField文本框中开始编辑的时候显示清除设置，点击清除按钮删除内容
    phoneTF.delegate = self;// 谁来响应
    [BGview addSubview:phoneTF];
}

#pragma mark -- Switch
// 开关：开 关
-(void)sexswitch1{
    mswitch = [[UISwitch alloc]initWithFrame:CGRectMake(90, 180, 50, 25)];
    [mswitch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
    mswitch.on =true; //switch 默认状态
    [BGview addSubview:mswitch];
    
}
-(void)switchAction:(UISwitch*)sender{
    //NSLog(@"%d",[sender isOn]);
    //isproSW1On = [_proSW1 isOn];
    //NSLog(@"isproSW1On 现在的状态 %id",issexSW1On);
    issexSW1On = !issexSW1On;
    if (issexSW1On) {
        [self btmethodstatus:true];
    }else {
        [self btmethodstatus:false];
    }
}
-(void)btmethodstatus:(BOOL)status{
    if (status) {
        
        //NSLog(@"點擊了true ");
        sexLabel.text = @"女";
        
    }else{
        //NSLog(@"點擊了false ");
        sexLabel.text = @"男";
    }
    //NSLog(@" SWstatus %id",status); //按键
    NSLog(@"issexSW1On %id",issexSW1On); //状态
}

-(void)titlesexLabel{
    //  3 字体
    UILabel *Label1 = [[UILabel alloc]initWithFrame:CGRectMake(20, 170, 70, 50)];
    //label3.backgroundColor = MENUBROWN;// 背景色
    Label1.text = [titledic valueForKey:@"Prolabel3"];// 内容
    Label1.textColor = [UIColor grayColor]; //内容颜色
    //  使用字体名称
    //label3.font = [UIFont fontWithName:@"Aril-BoldMT" size:15.0];
    Label1.font = [UIFont systemFontOfSize:14.0];
    //  对其方式: text内容 相对 uilabel command＋鼠标左键
    Label1.textAlignment = NSTextAlignmentLeft;//NSTextAlignmentCenter;
    
    [BGview addSubview:Label1];
}

-(void)setsexLabel{
    //  3 字体
    sexLabel = [[UILabel alloc]initWithFrame:CGRectMake(160, 170, 70, 50)];
    //label3.backgroundColor = MENUBROWN;// 背景色
    sexLabel.text = @"女";// 内容
    sexLabel.textColor = [UIColor grayColor]; //内容颜色
    //  使用字体名称
    //label3.font = [UIFont fontWithName:@"Aril-BoldMT" size:15.0];
    sexLabel.font = [UIFont systemFontOfSize:16.0];
    //  对其方式: text内容 相对 uilabel command＋鼠标左键
    sexLabel.textAlignment = NSTextAlignmentLeft;//NSTextAlignmentCenter;
    
    [BGview addSubview:sexLabel];
}

-(void)setemailLabel{
    //  3 字体
    UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(20, 220, 70, 50)];
    //label3.backgroundColor = MENUBROWN;// 背景色
    label3.text =[titledic valueForKey:@"Prolabel4"];// 内容
    label3.textColor = [UIColor grayColor]; //内容颜色
    //  使用字体名称
    //label3.font = [UIFont fontWithName:@"Aril-BoldMT" size:15.0];
    label3.font = [UIFont systemFontOfSize:14.0];
    //  对其方式: text内容 相对 uilabel command＋鼠标左键
    label3.textAlignment = NSTextAlignmentLeft;//NSTextAlignmentCenter;
    
    [BGview addSubview:label3];
}

// textfiled
-(void)setemailTF{
    emailTF = [[UITextField alloc]initWithFrame:CGRectMake(90, 220, (UISCREEN_WIDTH  - 60 - 90), 40)];
    emailTF.borderStyle = UITextBorderStyleLine;// 边框
    //text2.textColor = [UIColor redColor];// text 字体颜色 内容字体大小
    //nameTF.secureTextEntry = YES; //密码
    emailTF.font = [UIFont systemFontOfSize:17.0];
    //nameTF.placeholder = @"請輸入密碼";// 提示
    
    //    UITextField *textfield = [[UITextField alloc]initWithFrame:CGRectMake(10, 50, 300, 30)];
    //    textfield.borderStyle = UITextBorderStyleLine;// 边框
    //    textfield.textColor = [UIColor redColor];// text 字体颜色 内容字体大小
    //    textfield.font = [UIFont systemFontOfSize:17.0];
    //    textfield.placeholder = @"please enter text";// 提示
    
    emailTF.keyboardType = UIKeyboardTypeEmailAddress;
    //    UIKeyboardTypeDefault,                // Default type for the current input method.
    //    UIKeyboardTypeASCIICapable,           // Displays a keyboard which can enter ASCII characters
    //    UIKeyboardTypeNumbersAndPunctuation,  // Numbers and assorted punctuation.
    //    UIKeyboardTypeURL,                    // A type optimized for URL entry (shows . / .com prominently).
    //    UIKeyboardTypeNumberPad,              // A number pad with locale-appropriate digits (0-9, ۰-۹, ०-९, etc.). Suitable for PIN entry.
    //    UIKeyboardTypePhonePad,               // A phone pad (1-9, *, 0, #, with letters under the numbers).
    //    UIKeyboardTypeNamePhonePad,           // A type optimized for entering a person's name or phone number.
    //    UIKeyboardTypeEmailAddress,           // A type optimized for multiple email address entry (shows space @ . prominently).
    //    UIKeyboardTypeDecimalPad NS_ENUM_AVAILABLE_IOS(4_1),   // A number pad with a decimal point.
    //    UIKeyboardTypeTwitter NS_ENUM_AVAILABLE_IOS(5_0),      // A type optimized for twitter text entry (easy access to @ #)
    //    UIKeyboardTypeWebSearch NS_ENUM_AVAILABLE_IOS(7_0),    // A default keyboard type with URL-oriented addition (shows space . prominently).
    //    UIKeyboardTypeASCIICapableNumberPad NS_ENUM_AVAILABLE_IOS(10_0),
    emailTF.returnKeyType = UIReturnKeyDone;
    //设置UITextField的边框样式
    emailTF.borderStyle = UITextBorderStyleRoundedRect;//圆角
    //    UITextBorderStyleNone, // 无边框
    //    UITextBorderStyleLine, // 有黑色边框
    //    UITextBorderStyleBezel, // 有灰色边框和阴影
    //    UITextBorderStyleRoundedRect // 圆角
    
    emailTF.clearButtonMode = UITextFieldViewModeWhileEditing; //输入框右侧删除按钮  在UITextField文本框中开始编辑的时候显示清除设置，点击清除按钮删除内容
    emailTF.delegate = self;// 谁来响应
    [BGview addSubview:emailTF];
}

#pragma mark -- submitBtn
-(void)submitBtn{
    //   1 UI实例化 2 UI属性 3view 添加
    //initWithFrame:UI实例化 Frame
    //  UI+XX
    //    NSLog(@"_BGview.bounds.size.width %f",_BGview.bounds.size.width);
    //    NSLog(@"UISCREEN_WIDTH %f",UISCREEN_WIDTH);
    //UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake((_BGview.bounds.size.width - 120)/2 , 370, 140, 40)]; //(_BGview.bounds.size.width - 140)/2
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake((UISCREEN_WIDTH - 140 - 20)/2 , 280, 140, 40)];
    //  作用：为Button 添加一个内容 1:title 2状态
    [button setTitle:[titledic valueForKey:@"submit"] forState:UIControlStateNormal];
    //  作用：为Button 内容设置颜色 1:title颜色 2状态
    [button setTitleColor:MENUBROWN forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize: 16.0];  //字體大小
    
    [button setBackgroundImage:[UIImage imageNamed:@"botton_140x40.jpg"] forState:UIControlStateNormal];
    
    //但是问题又出来，此时文字会紧贴到做边框，我们可以设置
    //button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    
    //使文字距离做边框保持10个像素的距离。
    //button.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0);
    
    
    
    //  作用：是为button添加响应方法 1 self 2@selector(方法名) 3UIControlEventTouchUpInside:点击之后
    //  :
    [button addTarget:self action:@selector(submitBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [BGview addSubview:button];
}

//  谁调用 bt 谁传递过来的
-(void)submitBtnAction{
    NSLog(@"Promotionsend");
    
    NSMutableDictionary *dic = [self submitAction];
    NSLog(@"提交字典  %@",dic);
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"Promotionsend" object:nil userInfo:dic];
}


-(NSMutableDictionary*)submitAction{
    [nameTF resignFirstResponder];
    [phoneTF resignFirstResponder];
    [emailTF resignFirstResponder];
    NSString *prostr1 = [nameTF.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSString *prostr2 = [phoneTF.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSString *prostr4 = [emailTF.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSString *sexstr = @"";
    if (issexSW1On) { //性别值
        sexstr = @"女";
    }else{
        sexstr = @"男";
    }
    
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setValue:prostr1 forKey:@"name"];
    [dic setValue:prostr2 forKey:@"phone"];
    [dic setValue:prostr4 forKey:@"email"];
    [dic setValue:sexstr forKey:@"sex"];
    return dic;
}


@end
